<template>
  <el-form ref="intoOut" :model="intoOut" :rules="rules" label-width="240px">
    <el-card header="节点启动策略">
      <el-form-item label="节点启动方式" prop="StartMode">
        <enumItem
          v-model="intoOut.StartMode"
          :readonly="readonly"
          :dic-key="EnumDic.FlowNodeStartMode"
          placeholder="节点启动方式"
          sys-head="flow"
          @change="startChange"
        />
      </el-form-item>
      <el-form-item v-if="intoOut.StartMode==2" label="延迟时间" prop="DelayTime">
        <el-input-number v-model="intoOut.DelayTime" placeholder="延迟时间(分钟)" controls-position="right" />
        <span style="margin-left: 20px; color: red;">*已节点开始日期计算</span>
      </el-form-item>
      <el-form-item v-else-if="intoOut.StartMode==1" label="读取的字段" prop="StartColId">
        <formColChiose v-model="intoOut.StartColId" :cur-form-id.sync="intoOut.StartFormId" placeholder="读取启动时间的表单字段" :form-id="formId" :clearable="true" />
      </el-form-item>
    </el-card>
    <el-card header="出方向策略" style="margin-top: 10px">
      <el-form-item label="存在多个可入节点时" prop="OutPolicy">
        <enumItem
          v-model="intoOut.OutPolicy"
          :readonly="readonly"
          :dic-key="EnumDic.FlowNodeOutPolicy"
          placeholder="存在多个可入节点时处理方式"
          sys-head="flow"
        />
      </el-form-item>
      <el-form-item label="节点出条件">
        <enumItem
          v-model="intoOut.OutCondition"
          :readonly="readonly"
          :dic-key="EnumDic.FlowOutCondition"
          placeholder="节点出条件"
          sys-head="flow"
        />
      </el-form-item>
      <el-form-item label="未通过时的处理方式">
        <enumItem
          v-model="intoOut.OutNoPassOpt"
          :readonly="readonly"
          :dic-key="EnumDic.AuditNoPassOpt"
          placeholder="审批未通过时的处理方式"
          sys-head="flow"
        />
      </el-form-item>
      <el-form-item v-if="intoOut.OutCondition !== 0" label="是否等待子流程通过">
        <el-switch
          v-model="intoOut.OutIsWaitPass"
          :disabled="readonly"
          active-text="是"
          inactive-text="否"
        />
      </el-form-item>
      <el-form-item v-if="intoOut.OutCondition == 3 || intoOut.OutCondition == 2" label="子流程最小通过数" prop="OutMinPassNum">
        <el-input-number
          v-model="intoOut.OutMinPassNum"
          :disabled="readonly"
          placeholder="子流程最小通过数"
        />
      </el-form-item>
      <el-form-item
        v-if="intoOut.OutCondition == 2"
        label="选择需完成的子流程"
      >
        <el-select
          v-model="intoOut.OutWaitNodeId"
          :disabled="readonly"
          placeholder="选择需完成的子流程"
          :clearable="true"
        >
          <el-option
            v-for="t in filterSubNode()"
            :key="t.Id"
            :label="t.NodeName"
            :value="t.Id"
          />
        </el-select>
      </el-form-item>
    </el-card>
    <el-card header="入方向策略" style="margin-top: 10px">
      <el-form-item label="节点进入条件">
        <enumItem
          v-model="intoOut.IntoCondition"
          :readonly="readonly"
          :dic-key="EnumDic.FlowNodeIntoCondition"
          placeholder="节点进入条件"
          sys-head="flow"
        />
      </el-form-item>
      <el-form-item v-if="intoOut.IntoCondition == 2 || intoOut.IntoCondition == 3" label="进入的最小通过数" prop="IntoMinPassNum">
        <el-input-number
          v-model="intoOut.IntoMinPassNum"
          :disabled="readonly"
          placeholder="进入的最小通过数"
        />
      </el-form-item>
      <el-form-item
        v-if="intoOut.IntoCondition == 3"
        label="选择需完成的上级流程"
      >
        <el-select
          v-model="intoOut.IntoWaitNodeId"
          :disabled="readonly"
          placeholder="选择需完成的上级流程"
          :clearable="true"
        >
          <el-option
            v-for="t in source.FromNode"
            :key="t.Id"
            :label="t.NodeName"
            :value="t.Id"
          />
        </el-select>
      </el-form-item>
      <el-form-item
        v-for="item in formulaList"
        :key="item.NodeId"
        :label="item.NodeName + '节点条件公式'"
      >
        <nodeFormulaEdit
          :key="item.NodeId"
          v-model="item.formula"
          :readonly="readonly"
          :node-id="item.NodeId"
          :def-form-id="defFormId"
          :node-name="item.NodeName"
        />
      </el-form-item>
    </el-card>
    <div v-if="readonly==false" style="text-align: center; margin-top: 10px">
      <el-button type="primary" @click="saveInto">保存</el-button>
      <el-button @click="initIntoOut">重置</el-button>
    </div>
  </el-form>
</template>

<script>
import { GetFormId } from '@/flow/api/flow'
import { EnumDic,FlowNodeType } from '@/flow/config/flowConfig'
import * as flowNodeApi from '@/flow/api/flowNode'
import nodeFormulaEdit from './nodeFormulaEdit.vue'
import formColChiose from '@/customForm/components/form/formColChiose.vue'

export default {
  components: {
    nodeFormulaEdit,
    formColChiose
  },
  props: {
    nodeId: {
      type: String,
      default: null
    },
    defFormId: {
      type: String,
      default: null
    },
    flowId: {
      type: String,
      default: null
    },
    source: {
      type: Object,
      default: null
    },
    isload: {
      type: Boolean,
      default: false
    },
    readonly: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      EnumDic,
      intoOut: {},
      formId: [],
      formulaList: [],
      rules: {
        IntoMinPassNum: [
          {
            required: true,
            message: '进入的最小通过数不能为空！',
            trigger: 'blur'
          }
        ],
        OutMinPassNum: [
          {
            required: true,
            message: '子流程的最小通过数不能为空！',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  computed: {},
  watch: {
    isload: {
      handler(val) {
        if (val) {
          this.initIntoOut()
        }
      },
      immediate: true
    }
  },
  mounted() {

  },
  methods: {
    async startChange() {
      if (this.intoOut.StartMode === 1 && this.formId.length === 0) {
        this.formId = await GetFormId(this.flowId)
      }
    },
    filterSubNode() {
      console.log(this.source.ToNode)
      if (this.source.ToNode == null || this.source.ToNode.length === 0) {
        return []
      }
      return this.source.ToNode.filter((c) => c.NodeType === FlowNodeType.subFlow.value)
    },
    initIntoOut() {
      this.intoOut = {
        StartMode: this.source.StartMode,
        StartFormId: this.source.StartFormId,
        StartColId: this.source.StartColId,
        DelayTime: this.source.DelayTime,
        IntoCondition: this.source.IntoCondition,
        IntoMinPassNum: this.source.IntoMinPassNum,
        OutIsWaitPass: this.source.OutIsWaitPass,
        IntoWaitNodeId: this.source.IntoWaitNodeId,
        OutPolicy: this.source.OutPolicy,
        OutCondition: this.source.OutCondition,
        OutMinPassNum: this.source.OutMinPassNum,
        OutNoPassOpt: this.source.OutNoPassOpt,
        OutWaitNodeId: this.source.OutWaitNodeId
      }
      this.index = 0
      if (this.source.FromNode != null) {
        this.formulaList = this.source.FromNode.map((c) => {
          const data = {
            NodeId: c.Id,
            NodeName: c.NodeName,
            FormId: c.FormId,
            formula: null
          }
          if (this.source.IntoFormula != null) {
            data.formula = this.source.IntoFormula.find(
              (a) => a.NodeId === c.Id
            )
          }
          return data
        })
      } else {
        this.formulaList = []
      }
    },
    saveInto() {
      const that = this
      this.$refs['intoOut'].validate((valid) => {
        if (valid) {
          that.saveIntoOut()
        } else {
          return false
        }
      })
    },
    async saveIntoOut() {
      const data = Object.assign({}, this.intoOut)
      if (this.formulaList != null && this.formulaList.length > 0) {
        const list = this.formulaList.filter(
          (a) => a.formula != null && a.formula.Formula != null
        )
        data.IntoFormula = list.map((a) => a.formula)
      }
      await flowNodeApi.SetIntoOut(this.nodeId, data)
      this.$message({
        message: '更新成功!',
        type: 'success'
      })
      this.$emit('change', data)
    }
  }
}
</script>
